/**
 * Created with IntelliJ IDEA.
 * Description:经典线程不安全
 * User: lenovo
 * Date: 2022-09-06
 * Time: 7:30
 */
class Counter{
    public  int count = 0;
    public void increase(){
        count++;
    }
}
public class Demo13 {
    public static Counter c= new Counter();
    // 搞俩线程, 每个线程都针对这个 counter 来进行 5w 次自增!!
    // 预期结果 10w !!!
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                c.increase();
            }
        },"这是线程1");

        Thread thread2 = new Thread(()->{
            for (int i = 0; i < 50000; i++) {
                c.increase();
            }
        },"这是线程2");

        thread1.start();
        thread2.start();
        //main线程阻塞,等待thread1和thread2执行完成
        thread1.join();
        thread2.join();
        //System.out.println(thread1.getState());
        System.out.println("count增加的次数:"+c.count);
    }
}
